package com.jboost.modules.app.service.strategry.pdm;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.BomBDInfo;
import com.jboost.modules.app.dto.JdyReq;
import com.jboost.modules.app.dto.PdmBanDanDetailReqDto;
import com.jboost.modules.app.dto.resp.BDCB;
import com.jboost.modules.app.dto.resp.BdDy;
import com.jboost.modules.app.dto.resp.BdSJ;
import com.jboost.modules.app.service.strategry.common.AbstractJdy;
import com.jboost.modules.system.entity.PdmBom;
import com.jboost.modules.system.entity.PdmPl;
import com.jboost.modules.system.mapper.PdmBomMapper;
import com.jboost.modules.system.mapper.PdmPlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;
import java.sql.Wrapper;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service(value = "PdmBomQuery")
@Slf4j
public class PdmBomQuery extends AbstractJdy<String, PdmBom, PdmBomMapper, PdmBom> {


    public String getFormId1() {
        return "651d5873fc1c1164757ca231";
    }

    public String getFormId2() {
        return "651b8660e3d8a889989d68c1";
    }

    public String getFormId() {
        return "64b89ecaf7997d00085e492e";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "j2tq9lljcelsqTPZnbr36USxr6yZWsHu");
        return jdyHeaders;
    }
    //64e1f2d0d8b6100009e88053

    public String getAppId() {
        return "64e1f2d0d8b6100009e88053";
    }




    @Scheduled(initialDelay = 1000, fixedDelay = 1440000)
    @Async(value = "taskExecutor")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        try {
            System.out.println("111111");
            sync(new PdmBom());

                try {
                    sync1();
                } catch (Exception e) {

                }


                try {
                    sync2();
                } catch (Exception e) {

                }

                try {
                   // sync3();
                } catch (Exception e) {

                }

            return BaseResult.success();
        }catch (Exception exception){

            System.out.println(1);

        }
        return BaseResult.success();
    }

    private void sync3() throws IOException {
        log.info("开始批量查询Hs材料数据");
        //baseMapper.delete((Wrapper<PdmPl>) Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId3());
        param.put("limit",200);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                try {
                    BomBDInfo bdDy = JSONUtil.fromJson(e1.toJSONString(), BomBDInfo.class);

                    PdmBom pdmBom = baseMapper.selectById(bdDy.get_widget_1695299408498());
                    String status = bdDy.get_widget_1695300314310();
                    if (Objects.equals(status,"正常")) {
                        if (Objects.isNull(pdmBom)) {
                            BomBDInfo.DataBeanReq dataBeanReq = new BomBDInfo.DataBeanReq();
                            dataBeanReq.set_widget_1695300314310(getParaMap("已取消"));
                            jdyUpdate3(bdDy.get_id(), dataBeanReq);
                        }
                    }


                }catch (Exception exception){
                    System.out.println(1);
                }

            });
            List<String> uuids = jsonObjects.stream().map(e -> JSONUtil.fromJson(e.toJSONString(), BomBDInfo.class)).map(e1 -> e1.get_widget_1695299408498()).collect(Collectors.toList());
            JdyReq jdyReq = new JdyReq();
            jdyReq.setApp_id(getAppId());
            jdyReq.setEntry_id(getFormId3());
            jdyReq.setLimit(500);
            jdyReq.setFields(Arrays.asList("_widget_1695299408498", "_widget_1695300314310", "_id"));
            JdyReq.FilterBean filterBean = new JdyReq.FilterBean();
            filterBean.setRel("and");
            List<JdyReq.FilterBean.CondBean> cond = new ArrayList<>();
            JdyReq.FilterBean.CondBean a = new JdyReq.FilterBean.CondBean();
            a.setField("_widget_1695299408498");
            a.setType("text");
            a.setMethod("in");
            a.setValue(uuids);
            cond.add(a);

            JdyReq.FilterBean.CondBean a1 = new JdyReq.FilterBean.CondBean();
            a1.setField("_widget_1695300314310");
            a1.setType("text");
            a1.setMethod("eq");
            a1.setValue(Arrays.asList("正常"));
            cond.add(a1);
            filterBean.setCond(cond);
            jdyReq.setFilter(filterBean);
            try {
                String s = jdyQueryAll(jdyReq);
                JSONArray jsonArray = (JSONArray) JSONUtil.fromJson(s, HashMap.class).get("data");

                Map<String, List<Object>> widget1695299408498 = jsonArray.stream().collect(Collectors.groupingBy(e -> ((JSONObject) e).getString("_widget_1695299408498")));
                widget1695299408498.values().forEach(e->{
                    if (e.size() > 1) {
                        for (int i = 0; i < e.size() - 1; i++) {
                            JSONObject jsonObject = (JSONObject) e.get(i);
                            String dataId = (String) jsonObject.get("_id");
                            PdmBanDanDetailReqDto pdmBanDanDetailReqDto = new PdmBanDanDetailReqDto();
                            pdmBanDanDetailReqDto.set_widget_1695300314310(getParaMap("已取消"));
                            try {
                                jdyUpdate3(dataId, pdmBanDanDetailReqDto);
                            } catch (IOException ex) {

                            }
                        }
                    }
                });

            } catch (IOException ex) {

            }
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);
    }


    private String getFormId3() {
        return "651fbb02391f2adff5ba5fd4";
    }

    public String jdyUpdate(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId1());
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }

    public String jdyUpdate2(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId2());
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }

    public String jdyUpdate3(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId3());
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }


    public void sync2() throws IOException {
        log.info("开始批量查询Hs材料数据");
        //baseMapper.delete((Wrapper<PdmPl>) Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId2());
        param.put("limit",1000);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                try {
                    BDCB.DataBean bdDy = JSONUtil.fromJson(e1.toJSONString(), BDCB.DataBean.class);
                    String kh = bdDy.get_widget_1697645023695();
                    String wlbm = bdDy.get_widget_1696302725699();
                    String yszwmc = bdDy.get_widget_1696302725701();

                    List<PdmBom> pdmBoms = baseMapper.selectList(Wrappers.lambdaQuery(PdmBom.class).eq(PdmBom::getKh, kh).eq(PdmBom::getWlbm, wlbm).eq(PdmBom::getYsdzwmc, yszwmc));
                    if (CollectionUtils.isEmpty(pdmBoms) && !Objects.equals(bdDy.get_widget_1710302934901(),"是")
                            && !Objects.equals(bdDy.get_widget_1696302725696(),"已取消")){
                        BDCB.DataBeanReq dataBeanReq = new BDCB.DataBeanReq();
                        dataBeanReq.set_widget_1696302725696(getParaMap("已取消"));
                        jdyUpdate2(bdDy.get_id(),dataBeanReq);
                    }

                }catch (Exception exception){
System.out.println(1);
                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

    }
    public void sync1() throws IOException {
        log.info("开始批量查询Hs材料数据");
        //baseMapper.delete((Wrapper<PdmPl>) Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId1());
        param.put("limit",200);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                try {
                    BdDy.DataBean bdDy = JSONUtil.fromJson(e1.toJSONString(), BdDy.DataBean.class);
                    String wlbm = bdDy.get_widget_1695639159463();
                    String yszwmc = bdDy.get_widget_1711015798639();

                    List<PdmBom> pdmBoms = baseMapper.selectList(Wrappers.lambdaQuery(PdmBom.class).eq(PdmBom::getWlbm, wlbm).eq(PdmBom::getQhjd, yszwmc));
                    if (CollectionUtils.isEmpty(pdmBoms) && !Objects.equals(bdDy.get_widget_1710302538619(),"是")
                            && !Objects.equals(bdDy.get_widget_1695639321594(),"已取消")){
                        BdSJ.DataBeanReq dataBeanReq = new BdSJ.DataBeanReq();
                        dataBeanReq.set_widget_1695639321594(getParaMap("已取消"));
                        jdyUpdate(bdDy.get_id(),dataBeanReq);
                    }


                }catch (Exception exception){

                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

}

    public void sync(PdmBom entit) throws IOException {
        log.info("开始批量查询Hs材料数据");
        baseMapper.delete( Wrappers.lambdaQuery(PdmBom.class).ne(PdmBom::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("limit",500);
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<PdmBom> pdmBoms = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                try {
                    BdSJ.DataBean bdSJ = JSONUtil.fromJson(e1.toJSONString(), BdSJ.DataBean.class);
                    List<BdSJ.DataBean.Widget1692527689426Bean> widget1692527689426 = bdSJ.get_widget_1692527689426();
                    widget1692527689426.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692527689431());
                        pdmPl.setYsdzwmc(e.get_widget_1692527689433());
                        pdmPl.setId(e.get_widget_1695299267679());
                        pdmPl.setQhjd(e.get_widget_1699263621014());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1689825348641Bean> widget1689825348641 = bdSJ.get_widget_1689825348641();
                    widget1689825348641.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1689825348643());
                        pdmPl.setYsdzwmc(e.get_widget_1689825348645());
                        pdmPl.setId(e.get_widget_1695299267678());
                        pdmPl.setQhjd(e.get_widget_1699263621018());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1692856726767Bean> widget1692856726767 = bdSJ.get_widget_1692856726767();
                    widget1692856726767.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692856726772());
                        pdmPl.setYsdzwmc(e.get_widget_1692856726774());
                        pdmPl.setId(e.get_widget_1695299267680());
                        pdmPl.setQhjd(e.get_widget_1699263621015());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1692856726792Bean> widget1692856726792 = bdSJ.get_widget_1692856726792();
                    widget1692856726792.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692856726797());
                        pdmPl.setYsdzwmc(e.get_widget_1692856726799());
                        pdmPl.setId(e.get_widget_1695550458972());
                        pdmPl.setQhjd(e.get_widget_1699263621021());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1692856726817Bean> widget1692856726817 = bdSJ.get_widget_1692856726817();
                    widget1692856726817.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692856726822());
                        pdmPl.setYsdzwmc(e.get_widget_1692856726824());
                        pdmPl.setId(e.get_widget_1695550458975());
                        pdmPl.setQhjd(e.get_widget_1699263621024());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1692856726842Bean> widget1692856726842 = bdSJ.get_widget_1692856726842();
                    widget1692856726842.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692856726847());
                        pdmPl.setYsdzwmc(e.get_widget_1692856726849());
                        pdmPl.setId(e.get_widget_1695550458980());
                        pdmPl.setQhjd(e.get_widget_1699263621027());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });

                    List<BdSJ.DataBean.Widget1692856726867Bean> widget1692856726867 = bdSJ.get_widget_1692856726867();
                    widget1692856726867.forEach(e->{
                        PdmBom pdmPl = new PdmBom();
                        pdmPl.setWlbm(e.get_widget_1692856726872());
                        pdmPl.setYsdzwmc(e.get_widget_1692856726874());
                        pdmPl.setId(e.get_widget_1695550458985());
                        pdmPl.setQhjd(e.get_widget_1699263621061());
                        pdmPl.setType("1");
                        pdmPl.setKh(bdSJ.getStyle_number());
                        pdmBoms.add(pdmPl);
                    });



                }catch (Exception exception){

                    System.out.println(1);

                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);
        baseMapper.insertBatchSomeColumn(pdmBoms);

    }



}
